﻿using System;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace FlickrXplorer.Web.Views.Shared
{
    public partial class ImageListControl : ViewUserControl<ViewData.PhotoData>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.SaveUrl();
        }

        protected void SaveUrl()
        {
            string url = string.Empty;
            if (Context.Request != null)
            {
                url = Context.Request.Url.PathAndQuery;
                // at least two with style param.
                if (Context.Request.Form.Keys.Count > 1)
                {
                    StringBuilder builder = new StringBuilder(url);

                    if (url.IndexOf("?") > 0)
                        builder.Append("&");
                    else
                        builder.Append("?");

                    // iterate.
                    foreach (string key in Context.Request.Form.AllKeys)
                    {
                        if (string.Compare(key, "style", StringComparison.OrdinalIgnoreCase) != 0)
                        {
                            builder.Append(key + "=" + Context.Request.Form[key]);
                            builder.Append("&");
                        }
                    }
                    url =  builder.ToString();
                }
                // save it.
                SaveUrl(url);
            }
        }

        private void SaveUrl(string url)
        {
            HttpCookie cookie = new HttpCookie("url");
            cookie.Value = url;
            Context.Response.Cookies.Add(cookie);
        }
    }
}